<!--  -->
<template>
  <el-dialog title="停电用户修改" :close-on-click-modal="false" :visible.sync="yhEditDialogShow" width="400px" v-dialogDrag>
    <el-form v-if="yhEditDialogShow" :model="yhForm" :rules="yhFormRules" ref="yhForm" label-width="30%" size="mini">
      <el-form-item label="用户编码">
        <el-input style="width: 85%" v-model="yhForm.yhbm" size="mini" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input style="width: 85%" v-model="yhForm.yhmc" size="mini" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="集成起始时间" v-if="yhForm.jcqssj != null">
        <el-input style="width: 85%" v-model="yhForm.jcqssj" size="mini" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="集成终止时间" v-if="yhForm.jczzsj != null">
        <el-input style="width: 85%" v-model="yhForm.jczzsj" size="mini" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="集成持续时间" v-if="yhForm.jccxsj != null">
        <el-input style="width: 85%" v-model="yhForm.jccxsj" size="mini" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="停电起始时间" prop="qssj">
        <el-date-picker style="width: 85%" class="item-input-time" :readonly="monthReadonly" v-model="yhForm.qssj" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" type="datetime" placeholder="选择时间" size="mini"></el-date-picker>
      </el-form-item>
      <el-form-item label="停电终止时间" prop="zzsj">
        <el-date-picker style="width: 85%" class="item-input-time" :readonly="monthReadonly" v-model="yhForm.zzsj" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" type="datetime" placeholder="选择时间" size="mini"></el-date-picker>
      </el-form-item>
      <el-form-item label="停电容量" prop="tdrl">
        <el-input style="width: 85%" maxlength="12" v-model="yhForm.tdrl" size="mini" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"></el-input>KVA
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      
      <el-button @click="cancelYH" size="mini">关 闭</el-button>
      <el-button type="primary" @click="updateYH" size="mini" :loading="btnloading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateYxzyYhTemp } from "@/api/sjwh/yxzyyh.js";
import { checkFloatOrNumber } from "@/utils/formValidate";
import { getBetweenHour_b } from "@/utils/util.js";
export default {
  props: {
    updateYhList: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },

  data() {
    return {
      TOKEN: 0,
      sjId: "",//form表单事件id
      flagZs: "0",
      tempControl: "",
      czy: "",
      xdsj: {},
      yhEditDialogShow: false,
      monthReadonly: false,
      btnloading: false,
      yhForm: {
        //表单信息
        yhbm: "",
        yhmc: "",
        jcqssj: null,
        jczzsj: null,
        jccxsj: null,
        qssj: "",
        zzsj: "",
        tdrl: 0,
        zrl: 0, //用与判断tdrl能够修改的范围
      },
      yhList: {
        //父级传过来的用户信息
      },
      xdqssj: "",
      xdzzsj: "",
      yhFormRules: {
        qssj: [
          {
            required: true,
            message: "起始时间不能为空",
            trigger: "blur",
          },
        ],
        zzsj: [
          {
            required: true,
            message: "终止时间不能为空",
            trigger: "blur",
          },
        ],
        tdrl: [
          {
            required: true,
            message: "停电容量不能为空",
            trigger: "blur",
          },
          {
            validator: checkFloatOrNumber,
            trigger: "blur",
          },
        ],
      },
    };
  },

  components: {},

  computed: {},

  watch: {
    updateYhList(newVal, oldVal) {
    
      this.TOKEN = newVal.TOKEN,
      this.sjId = newVal.sjId,
      this.tempControl = newVal.temp;
      this.yhEditDialogShow = newVal.visibal;
      this.czy = newVal.czy;
      this.yhList = newVal.yhList;
      this.xdsj = newVal.xdsj;
      this.xdqssj = newVal.xdsj.qssj;
      this.xdzzsj = newVal.xdsj.zzsj;
      this.yhForm.yhbm = newVal.yhList.yhbm;
      this.yhForm.yhmc = newVal.yhList.yhmc;
      this.yhForm.qssj = newVal.yhList.qssj;
      this.yhForm.zzsj = newVal.yhList.zzsj;
      this.yhForm.tdrl = newVal.yhList.tdrl;
      this.yhForm.zrl = newVal.yhList.zrl;
    },
  },

  created() {},

  mounted() {},

  beforeUpdate() {
    //解决列配置引发的滚动条问题
  },

  update() {},

  destroyed() {},

  methods: {
    /**保存 */
    updateYH() {
  
      this.$refs["yhForm"].validate((valid) => {
   
        if (valid) {
          
          let qs = this.yhForm.qssj; //用户起始时间
          let zz = this.yhForm.zzsj; //用户终止时间

          let cxsj = Number(getBetweenHour_b(qs, zz)); //用户持续时间
       
          if (cxsj < 0) {
            this.$message({
              message: "终止时间必须大于起始时间",
              type: "warning",
            });
            return false;
          }
          let contrastQssj = getBetweenHour_b(qs, this.xdqssj);
          let contrastZzsj = getBetweenHour_b(zz, this.xdzzsj);
          if (contrastQssj > 0 || contrastZzsj < 0) {
            this.$message({
              message: "起始时间和终止时间必须在线段的停电时间范围内",
              type: "warning",
            });
            return false;
          }
       
          let tdrl = Number(this.yhForm.tdrl);
          //停电容量的范围
          let minZrl = 0;
          let maxZrl = this.yhForm.zrl;
          if (tdrl < minZrl || tdrl > maxZrl) {
            this.$message({
              message: `停电容量修改值不大于${maxZrl}`,
              type: "warning",
            });
            return false;
          }
          //调用用户修改接口
          this.updateYhItem()
        } else {
          this.$message({
            message: "请将必填字段填写完整",
            type: "warning",
          });
        }
      });
    },

    async updateYhItem() {
      try {
        this.btnloading = true;
        let cxsj = Number(getBetweenHour_b(this.yhForm.qssj,this.yhForm.zzsj));
      let reqData = {
        TOKEN: this.TOKEN,
        czy: this.czy,
        temp: this.tempControl,
        sjId: this.sjId,
        xdId: this.yhList.xdId,
        id: this.yhList.id,
        qssj: this.yhForm.qssj,
        zzsj: this.yhForm.zzsj,
        cxsj: cxsj,
        tdrl: this.yhForm.tdrl
      }
  
      let res = await updateYxzyYhTemp(reqData);
    
      if(res.code === 0 && res.data === true){
        this.$message.success("修改成功！");
        this.$emit('updateYhCallData',res.data);
        this.yhEditDialogShow = false;
        this.btnloading = false;
      }else{
        this.flagZs = "1";
        this.$message.error(res.message);
        this.btnloading = false;
      }
      } catch (error) {
        this.btnloading = false;
      }
      
    },

    /**取消 */
    cancelYH() {
      this.yhEditDialogShow = false;
      if(this.flagZs == "1"){
        this.$emit('updateYhCallData',true);
      }
    },
  },
};
</script>
<style lang='scss' scoped>
/deep/ .el-dialog__footer {
  padding: 5px 10px 5px;
}
.item-input-time{
  width: 226px !important;
}
</style>
